//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "adminActions\fn_ui_moderation_detach.sqf"
#include <core_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_isAdmin","_dialog","_player","_selected_index","_executed"];
PARAMS_2(_selected_index,_player);
if (GVAR(AdminCmds) == 1) then { 
	if (isnil "_player" || {isnull _player}) exitWith { hint localize "STR_MSO_AA_NOSELECTION"; };
	_isAdmin = serverCommandAvailable "#kick" || !isMultiplayer;
	if (!isnil "mp_rights_fnc_isAdmin") then {
		_isAdmin = _isAdmin || ([] call mp_rights_fnc_isAdmin) || ([] call mp_rights_fnc_isModerator);
	};
	if (_isAdmin) then {
		if (alive player) then {
			if (alive _player) then {
				_executed = false;
				if !(isNull (attachedTo _player)) then {
					detach _player;
					systemChat format [localize "STR_MSO_AA_DETACHED", name _player];
					_executed = true;
				} else {
					DEBUG_1("Not attached to anything",_player);
				};
				if (!_executed && {({isPlayer _x} count (attachedObjects _player)) > 0}) then {
					{ if (isPlayer _x) then { detach _x; _executed = true; systemChat format [localize "STR_MSO_AA_DETACHED", name _x]; };
					} forEach (attachedObjects _player);
				} else {
					if (!_executed) then { DEBUG_1("Nothing attached to the player",_player); };
				};
				if (!_executed) then {
					[QGVAR(SWITCHMOVE),[_player,"AmovPercMstpSrasWrflDnon", name player],"all"] call FUNC(raiseEvent);
					DEBUG_1("Sending Switch Move",_player);
				};
			} else {
				hint format [localize "STR_MSO_AA_DETACH_NOPLAYER"];
			};
		};
	} else {
		hint localize "STR_MSO_AA_CMD_NA";
	};	
};

TRACE("End");
TMFPROFILERSTOP;
